package com.shengjing.apk.urlprocessor;

import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

import com.march.socialsdk.exception.SocialError;
import com.march.socialsdk.listener.OnShareListener;
import com.march.socialsdk.manager.ShareManager;
import com.march.socialsdk.model.ShareObj;
import com.march.socialsdk.platform.Target;
import com.shengjing.apk.util.SystemUtil;

public class OBJCProcessor {
    private static final String TAG = "OBJCProcessor";

    public static void process(WebView webview, String url) {
        Log.d(TAG, "url " + url);
        if (url.contains("share")) {
            share(webview, url);
        } else if (url.contains("clear")) {
            clear(webview);
        } else if (url.contains("copy")) {
            copy(webview, url);
        }
    }

    //objc://share4,abc,objc://share4,abc,  share1,share2,share3,share4 (微信,朋友圈,...)
    //objc://share4,http://47.104.31.171:8020/code_png/NTDwbjHmpU.png
    private static void share(WebView webview, String url) {
        String imageURL = url.substring(url.indexOf(",") + 1);
        if (!imageURL.startsWith("http")) {
            imageURL = "http://" + imageURL;
        }
        ShareObj textObj = ShareObj.buildImageObj(imageURL);
        int shareTarget = -1;
        if (url.contains("objc://share1")) {
            shareTarget = Target.SHARE_WX_FRIENDS;
        } else if (url.contains("objc://share2")) {
            shareTarget = Target.SHARE_WX_ZONE;
        }
        if (shareTarget == -1) return;
        ShareManager.share(webview.getContext(), shareTarget, textObj, new OnShareListener() {
            @Override
            public void onStart(int shareTarget, ShareObj obj) {
                Log.d(TAG, "onStart");
            }

            @Override
            public ShareObj onPrepareInBackground(int shareTarget, ShareObj obj) throws Exception {
                Log.d(TAG, "onPrepareInBackground");
                return null;
            }

            @Override
            public void onSuccess() {
                Log.d(TAG, "onSuccess");
            }

            @Override
            public void onFailure(SocialError e) {
                Log.d(TAG, "onFailure: " + e);
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "onCancel");
            }
        });
    }

    //objc://clear
    private static void clear(WebView webview) {
        webview.clearCache(true);
        Toast.makeText(webview.getContext(), "缓存清理完成", Toast.LENGTH_LONG).show();
    }

    //objc://copy,123  123是要复制的邀请码
    private static void copy(WebView webview, String url) {
        String value = url.replace("objc://copy,", "");
        SystemUtil.putTextIntoClip(webview.getContext(), value);
        Toast.makeText(webview.getContext(), "已经复制到剪切板", Toast.LENGTH_LONG).show();
    }
}
